'use strict';

var axios = require('axios')
var htmlParser = require('./home/html.js')
var weatherUtil = require('./home/weather.js')
var movieUtil = require('./home/movie.js')
var juejinUtil = require('./home/juejin.js')
var mapsUtil = require('./home/maps.js')
const openAiUtil = require("./home/openai.js");
const sendmail = require("./login/mail.js");
const sendsms = require("./login/mobile.js");
const getwxconfig = require("./wechat/config.js");

exports.main = async (event, context) => {
	//event为客户端上传的参数
	let {CLIENTIP:ip_info}=context
	console.log('\nevent : ', event,ip_info)
	console.log("\ncontext",context)
	let type = event.type||'login'
	var url = null;
	switch (event.action) {
		case 'getMovieComment':
			// url = 'https://zhaoyj.work/poetry/movie/detail?movieId=' + event.movie_id;
			return movieUtil.getMovieComment(event.movie_id)
			break;
		case 'getMovieDetail':
			// url = 'https://zhaoyj.work/poetry/movie/detail?movieId=' + event.movie_id;
			return movieUtil.getMovieDetail(event.movie_id)
			break;
		case 'getMovieShowing':
			// url = "https://zhaoyj.work/poetry/movie/showing";
			return movieUtil.getMovieShowing()
			break;
		case 'getMovieTop250':
			url = 'https://zhaoyj.work/poetry/movie/top250?limit=10&offset=' + event.start;
			break
		case "getAdArticleList":
			let menuKey = ''
			if (event.key)
				menuKey = `?k=${event.key}`
			url = "https://wanandroid.com/wxarticle/list/" + `${event.menuId}/${event.pageNum}/json${menuKey}`
			// console.log('getAdArticleList', url)
			break
		case "getWeiXinHtml":
			return htmlParser.getWeiXinHtml(event.url)
		case "getOneNote":
			url = "https://v1.hitokoto.cn/?c=d&c=e&c=h&c=i&c=j&c=k&c=l"
			break
		case "getWeatherData":
			return weatherUtil.getWeatherData(event.city)
			break
		case "sendEmailCode":
			let mail = event.mail
			return sendmail(mail,type)
			break
		case "sendMobileCode":
			let mobile = event.mobile
			return sendsms(mobile,type)
			break
		case "getWeChatConfig":
			let href = event.href
			return getwxconfig(href)
			break
		case "convertLocation":
			let location = event.location
			url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' +
				`${location.latitude},${location.longitude}&key=MHLBZ-EWRCK-BKGJL-A5CQW-6SVT3-Q5B7E`
			break
		case "checkMapInfo":
			return mapsUtil.checkMapInfo(event.type)
			break
		case "juejinApi":
			return juejinUtil.checkIn()
			break
		case "openAI":
            return openAiUtil.getOpenAiData({...event.params,ip_info})
            break
		default:
			return juejinUtil.checkIn()
	}
	let {data}=await axios.get(url)
	return  data
};
